package com.ntschy.web.controller.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.common.constant.HttpStatus;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import com.ntschy.common.core.entity.dataobject.SysDictData;
import com.ntschy.sys.entity.dto.dictdata.SysDictDataAddUpdateDTO;
import com.ntschy.sys.entity.dto.dictdata.SysDictDataPageDTO;
import com.ntschy.sys.entity.mapstruct.SysDictDataMapstruct;
import com.ntschy.sys.entity.valid.SysDictDataGroups;
import com.ntschy.sys.entity.valid.SysDictTypeGroups;
import com.ntschy.sys.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author ezreal_geng
 */
@RestController
@RequestMapping("/sys/dictData")
@Api(tags = "字典数据接口")
@Validated
public class SysDictDataController extends BaseController<SysDictData> {

    @Autowired
    private ISysDictDataService dictDataService;

    // * ================================================基础增删改查BEGIN============================================= *
    @ApiOperation(value = "字典数据分页查询", notes = "")
    @ApiImplicitParam(name = "pageDTO", value = "字典类型分页查询对象", required = true, dataType = "SysDictDataPageDTO")
    //@PreAuthorize("@ss.hasPermi('sys:dictData:page')")
    @PostMapping("/pageDictData")
    public AjaxPageResult<SysDictData> pageDictData(@RequestBody SysDictDataPageDTO pageDTO) {
        Page<SysDictData> page = PageHelper.startPage(pageDTO.getCurrPage(), pageDTO.getPageSize());
        SysDictData dictData = SysDictDataMapstruct.SYS_DICT_DATA_MAP_STRUCT.pageDtoToDo(pageDTO);
        List<SysDictData> dictTypeList = dictDataService.pageDictData(dictData);
        return buildPageResult(dictTypeList, page);
    }

    @ApiOperation(value = "新增字典数据", notes = "")
    @ApiImplicitParam(name = "addDto", value = "字典数据新增实体类", required = true, dataType = "SysDictDataAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:dictData:add')")
    @PostMapping("/addDictData")
    public AjaxResult<String> addDictData(@Validated(SysDictDataGroups.Add.class) @RequestBody SysDictDataAddUpdateDTO addDto) {
        if(dictDataService.checkDictTypeIsSystemByDictType(addDto.getDictType())) {
            return AjaxResult.error(HttpStatus.ERROR, "系统内置字典类型不允许新增");
        }
        SysDictData addDictData = SysDictDataMapstruct.SYS_DICT_DATA_MAP_STRUCT.addUpdateDtoToDo(addDto);
        return toAjax(dictDataService.addDictData(addDictData), "新增成功", "新增失败");
    }

    @ApiOperation(value = "更新字典数据", notes = "")
    @ApiImplicitParam(name = "updateDto", value = "字典数据更新实体类", required = true, dataType = "SysDictDataAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:dictData:update')")
    @PostMapping("/updateDictData")
    public AjaxResult<String> updateDictData(@Validated(SysDictTypeGroups.Update.class) @RequestBody SysDictDataAddUpdateDTO updateDto) {
        if(dictDataService.checkDictTypeIsSystemByDictType(updateDto.getDictType())) {
            return AjaxResult.error(HttpStatus.ERROR, "系统内置字典类型不允许修改");
        }
        SysDictData updateDictData = SysDictDataMapstruct.SYS_DICT_DATA_MAP_STRUCT.addUpdateDtoToDo(updateDto);
        return toAjax(dictDataService.updateDictData(updateDictData), "更新成功", "更新失败");
    }

    @ApiOperation(value = "根据字典数据ID删除字典数据", notes = "")
    @ApiImplicitParam(name = "dictDataId", value = "字典数据ID", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:dictData:delete')")
    @GetMapping("/deleteDictData")
    public AjaxResult<String> deleteDictData(@NotBlank(message = "字典数据ID不能为空") @Length(min = 36, max = 36, message = "字典数据ID不合法") String dictDataId) {
        if(dictDataService.checkDictTypeIsSystemByDictDataCode(dictDataId)) {
            return AjaxResult.error(HttpStatus.ERROR, "系统内置字典类型不允许删除");
        }
        return toAjax(dictDataService.deleteDictData(dictDataId), "字典数据成功", "字典数据失败");
    }
    // * =================================================基础增删改查END============================================== *
}
